package com.yc.home.cloudother.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yc.entity.Createclass;
import com.yc.home.cloudother.mapper.CreateclassMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashSet;

@Component
@ServerEndpoint("/classs/{id}/{cid}/{status}")
@Slf4j
public class WebSocketController {

    //存储用户会话
    static HashSet<Session> sessions = new HashSet<>();

    public static CreateclassMapper createclassMapper;

    @Autowired
    public void setUserMapper(CreateclassMapper createclassMapper) {
        WebSocketController.createclassMapper = createclassMapper;
    }


    //连接
    @OnOpen
    public void onOpen(Session session, @PathParam("id") String id, @PathParam("cid") String cid, @PathParam("status") String status) throws IOException {
        //在会话中存入id
        //存取用户所需信息
        MyInfo myInfo = new MyInfo();
        myInfo.setUid(id);
        myInfo.setCid(cid);
        myInfo.setStatus(status);
        session.getUserProperties().put("myInfo", myInfo);
        sessions.add(session);
    }

    //关闭
    @OnClose
    public void onClose(Session session) throws IOException {
        sessions.remove(session);
    }

    //消息提示
    @OnMessage
    public void onMessage(Session session, String message) throws IOException {
        //解析消息传过来的用户id
        MyInfo myInfo = (MyInfo) session.getUserProperties().get("myInfo");
        if("1".equals(myInfo.getStatus())){
            //管理员同意
            send(message);
        }else {
            //用户发送消息
//            查询cid
            LambdaQueryWrapper<Createclass> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Createclass::getNum, message);
            Createclass createclass = createclassMapper.selectOne(lqw);
            send1(createclass.getId());
        }
    }

    //发送修改方法
    public void send(String message) throws IOException {
        for (Session s : sessions) {
            MyInfo myInfo = (MyInfo) s.getUserProperties().get("myInfo");
            if(myInfo.getUid().equals(message)){
                //将msgInfo转换成json
                String message1 = new ObjectMapper().writeValueAsString("刷新");
                s.getBasicRemote().sendText(message1);
            }
        }
    }
    //发送修改方法
    public void send1(Integer message) throws IOException {
        for (Session s : sessions) {
            MyInfo myInfo = (MyInfo) s.getUserProperties().get("myInfo");
            if(myInfo.getCid().equals(message.toString())){
                //将msgInfo转换成json
                String message1 = new ObjectMapper().writeValueAsString("刷新");
                s.getBasicRemote().sendText(message1);
            }
        }
    }
}

//发送用户信息
@Data
@AllArgsConstructor
@NoArgsConstructor
class MsgInfo{
    //发送人
    MyInfo myInfo;

    //文字消息
    String message;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class MyInfo{
    String uid;  //用户id
    String cid; //班级id
    String status; //用户头像
}
